//
//  DoctorDetailController.m
//  QuickCure1
//
//  Created by Haozhong Chen on 2016/11/3.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "DoctorDetailController.h"
#import "FocusDoctorDetailData.h"
#import "FocusDoctorData.h"
#import "DataBaseManager.h"
#import "AppDelegate.h"
#import "DoctorDetailView.h"
#import "NetWorkTool.h"

@interface DoctorDetailController ()
@property(nonatomic,strong)NSMutableArray<FocusDoctorData*>*dataList;
@property(nonatomic,strong)FocusDoctorDetailData*detailData;
@end

@implementation DoctorDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadDetailData];
    
    DoctorDetailView*view=[DoctorDetailView creatDoctorDetailView];
    self.view=view;
    view.data=self.data;
    view.detailData=self.detailData;
    
    
    NSInteger doctor_id=[self.data.doctor_id integerValue];
    UIBarButtonItem*item;
    BOOL isExisted=[self queryDataBaseWithDoctor_ID:doctor_id];
    if(isExisted)
    {
        item=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"shixin"] style:UIBarButtonItemStylePlain target:self action:@selector(clickCollectButon:)];
    }
    else
    {
        
        item=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"kongxin"] style:UIBarButtonItemStylePlain target:self action:@selector(clickCollectButon:)];
    }


    
    
    
    [self.navigationItem setRightBarButtonItem:item];
    self.title=@"医生的基本信息";
    
    
   
}

-(NSMutableArray<FocusDoctorData*>*)dataList
{
    if (!_dataList)
    {
        _dataList=[[NSMutableArray alloc]init];
    }
    return _dataList;
}

#pragma mark - 获取医生详情数据
-(void)loadDetailData
{
    [self test9];
    [self test10];
    [self test11];
    
    
}
#pragma mark - 获取网络数据

-(void)test9{
    
    //请求地址: http://iosapi.itcast.cn/doctorReceivingSetting.json.php
    //    请求参数:{"doctor_id":300000315}
    
    NSString *urlString = kDoctorReceivingSetting;
    NSDictionary *paramDic = @{
                               @"doctor_id":@300000315
                               };

    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        
        
        NSDictionary*dict=responseObject;
        
        NSDictionary*dictData=[dict objectForKey:@"data"];
 
        [self.detailData setValuesForKeysWithDictionary:dictData];

        
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
}


-(void)test10{
    
    //10.医生简介接口
    //请求地址: http://iosapi.itcast.cn/getIntroduction.json.php
    //请求参数:{"doctor_id":300000315}
    
    NSString *urlString = kGetIntroductionUrl;
    NSDictionary *paramDic = @{
                               @"doctor_id":@300000315
                               };
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        
        NSDictionary*dict=responseObject;
        
        NSDictionary*dictData=[dict objectForKey:@"data"];
        
        
        [self.detailData setValuesForKeysWithDictionary:dictData];
        
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
    
}


-(void)test11{
    
//    11.获取医生就诊时间接口
//    请求地址: http://iosapi.itcast.cn/doctorDuties.json.php
//    请求参数:{"doctor_id":300000315}
    
    NSString *urlString = kDoctorDuties;
    NSDictionary *paramDic = @{
                               @"doctor_id":@300000315
                               };
    
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        
//        NSLog(@"%@",responseObject);
        //需要处理数据
//        NSDictionary*dict=responseObject;
//        NSDictionary*dictData=dict[@"data"];
//        NSArray<FocusDoctorData *> *modelArray = [NSArray yy_modelArrayWithClass:[FocusDoctorData class] json:array];
        
        
//        [self.dataList addObjectsFromArray:modelArray];
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];

    
}


#pragma mark - 添加收藏
-(void)clickCollectButon:(UIBarButtonItem*)item
{
    NSInteger doctor_id=[self.data.doctor_id integerValue];
    BOOL isExisted=[self queryDataBaseWithDoctor_ID:doctor_id];
    if(isExisted)
    {
        [self deleteDoctorTableWithDoctor_ID:doctor_id];
        [item setImage:[UIImage imageNamed:@"kongxin"]];
        [self showAlertVCWithDoctorNameCancel:self.data.doctor_name];
    }
    else
    {
        
        [self.dataList addObject:self.data];
        
        [item setImage:[UIImage imageNamed:@"shixin"]];
        
        
        [self addDoctorToDataListWithDataList:self.dataList.copy];
        [self showAlertVCWithDoctorName:self.data.doctor_name];
        [self.dataList removeAllObjects];
    }
  
}




-(void)addDoctorToDataListWithDataList:(NSArray<FocusDoctorData*>*)dataList
{
    [self initDataBaseWithModelArray:dataList];
    
    
}

#pragma mark - 弹窗提示
-(void)showAlertVCWithDoctorName:(NSString*)doctorName;
{
    //弹框
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:doctorName message:@"关注成功" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sureBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVc addAction:sureBtn];
    AppDelegate*delegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [delegate.window.rootViewController presentViewController:alertVc animated:YES completion:^{
    }];
}

-(void)showAlertVCWithDoctorNameCancel:(NSString*)doctorName;
{
    //弹框
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:doctorName message:@"取消关注" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sureBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertVc addAction:sureBtn];
    AppDelegate*delegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [delegate.window.rootViewController presentViewController:alertVc animated:YES completion:^{
    }];
}

#pragma mark - 数据库操作
-(void)initDataBaseWithModelArray:(NSArray<FocusDoctorData*>*)modelArray
{
    

    for (int i=0; i<modelArray.count; i++)
    {
        [[DataBaseManager sharedManager].dataBaseQueue inDatabase:^(FMDatabase *db) {
            
            BOOL isSucceed=[db executeUpdateWithFormat:@"INSERT INTO doctor (accuracy,banner,doctor_gender,doctor_hospital_name,doctor_id,doctor_name,doctor_portrait,doctor_title_name,easymob_id,flower,operation_count) VALUES (%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@);",modelArray[i].accuracy,modelArray[i].banner,modelArray[i].doctor_gender,modelArray[i].doctor_hospital_name,modelArray[i].doctor_id,modelArray[i].doctor_name,modelArray[i].doctor_portrait,modelArray[i].doctor_title_name,modelArray[i].easymob_id,modelArray[i].flower,modelArray[i].operation_count];
            NSLog(@"%@",isSucceed?@"OK":@"Failed");
        }];
    }
}
#pragma mark - 删除表中的某一项
-(void)deleteDoctorTableWithDoctor_ID:(NSInteger)doctor_id
{
    //DELETE FROM COMPANY WHERE ID = 7
    NSString*sqlString=[NSString stringWithFormat:@"delete from doctor where doctor_id=%ld",doctor_id];
    NSLog(@"%@",[[DataBaseManager sharedManager] executeSQLString:sqlString]?@"updata succeed":@"updata failed");
}

-(BOOL)queryDataBaseWithDoctor_ID:(NSInteger)doctor_id
{
    NSMutableArray<FocusDoctorData *> *array=[NSMutableArray array];
    NSString*sqlString=[NSString stringWithFormat:@"select * from doctor where doctor_id=%ld",doctor_id];
    NSArray*resultArray=[[DataBaseManager sharedManager] queryRecords:sqlString];
    NSArray<FocusDoctorData *> *modelArray = [NSArray yy_modelArrayWithClass:[FocusDoctorData class] json:resultArray];
    [array addObjectsFromArray:modelArray];
    if (array.count>0)
    {
        return YES;
    }
    else
        return NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
